#!/bin/bash

cd "$(dirname "$0")/.."

function checkout_at() {
  repo=$1; url=$2; sha=$3
  if [ ! -d "$repo" ]; then
    git clone "https://github.com/$url" "$repo"
  fi

  pushd "$repo"
  git fetch && git reset --hard "$sha"
  popd
}

# Define list of arguments expected in the input
optstring="s"

while getopts ${optstring} arg; do
  case ${arg} in
    s)
      SKIP_DOWNLOAD='true'
      echo "Skip download"
      ;;
    ?)
      echo "Invalid option: -${OPTARG}."
      ;;
  esac
done

if [[ $SKIP_DOWNLOAD != 'true' ]]; then
  checkout_at "examples/elm-spa-example" "rtfeldman/elm-spa-example" "c8c3201ec0488f17c1245e1fd2293ba5bc0748d5"
  checkout_at "examples/elm-browser" "elm/browser" "1d28cd625b3ce07be6dfad51660bea6de2c905f2"
  checkout_at "examples/elm-bytes" "elm/bytes" "2bce2aeda4ef18c3dcccd84084647d22a7af36a6"
  checkout_at "examples/elm-core" "elm/core" "65cea00afa0de03d7dda0487d964a305fc3d58e3"
  checkout_at "examples/elm-file" "elm/file" "e4ca3864c93a5e766e24ed6916174753567b2f59"
  checkout_at "examples/elm-html" "elm/html" "94c079007f8a7ed282d5b53f4a49101dd0b6cf99"
  checkout_at "examples/elm-http" "elm/http" "81b6fdc67d8e5fb25644fd79e6b0edbe2e14e474"
  checkout_at "examples/elm-json" "elm/json" "0206c00884af953f2cba8823fee111ee71a0330e"
  checkout_at "examples/elm-parser" "elm/parser" "7506b07eaa93a93d13b508b948c016105b0953c8"
  checkout_at "examples/elm-project-metadata-utils" "elm/project-metadata-utils" "831733724fb2b59b38ba1639e6503d97607dbd9d"
  checkout_at "examples/elm-random" "elm/random" "ecf97bb43f0d5cd75243428f69f45323957bda25"
  checkout_at "examples/elm-regex" "elm/regex" "8810c41fb17ddf89165665489be213f44070bc4a"
  checkout_at "examples/elm-svg" "elm/svg" "08bd432990862bab5b840654dd437fbb2e6176e7"
  checkout_at "examples/elm-time" "elm/time" "dc3b75b7366e59b99962706f7bf064d3634a4bba"
  checkout_at "examples/elm-url" "elm/url" "4e5ee032515581bf01428d54ee636dd601f4bc90"
  checkout_at "examples/elm-virtual-dom" "elm/virtual-dom" "44cbe2bf3d598cab569045cefcc10de31907598d"
  checkout_at "examples/elm-ui" "mdgriffith/elm-ui" "acae8857a02e600cc4b4737ca2f70607228b4489"
  checkout_at "examples/elm-markup" "mdgriffith/elm-markup" "b073d85490f71c6491648bcd0b11bf9aca6e53a5"
  checkout_at "examples/elm-visualization" "gampleman/elm-visualization" "6b9c7476507cedbbd8fc841fdecb59f4af2c3f96"
fi

skipped_files=()
examples_to_parse=()
all_examples=$(find examples -name '*.elm')
known_failures=$(cat script/known-failures.txt)
for example in $all_examples; do
  if [[ ! $known_failures == *$example* ]]; then
    examples_to_parse+=($example)
  else
    skipped_files+=($example)
  fi
done

start=`date +%s.%N`
tree_sitter_report=$(echo ${examples_to_parse[@]} | xargs -n 100000 npx tree-sitter parse --quiet --stat)
end=`date +%s.%N`

ret_code=$?

echo -e "-----------------------------------------------------------------\n$tree_sitter_report \n -----------------------------------------------------------------\n"

skipped=$( echo ${#skipped_files[@]} )
# This doesn't work on macos due to how date is used and will default to empty
runtime=$( echo "$end - $start" | bc -l )

printf "Skipped: %d \nTook: %s\n" $skipped $runtime

exit $ret_code
